home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4649 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: news.uni-jena.de!news
  2. From: mkt@isun04.inf.uni-jena.de (Tilo Koerbs)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help with Bug Pleeeeease!
  5. Date: 31 Jan 1996 13:53:36 GMT
  6. Organization: Lehrstuhl fuer Rechnerarchitektur- und kommunikation, FSU Jena
  7. Message-ID: <4ens90$qlr@fsuj01.rz.uni-jena.de>
  8. References: <tday-2801961838030001@tday.slip.netcom.com>
  9. Reply-To: mkt@isun04.inf.uni-jena.de
  10. NNTP-Posting-Host: isun07.inf.uni-jena.de
  11.  
  12. I found one error:
  13.  
  14. String String::operator+(const String & st) const
  15. {
  16.    String temp1;
  17.    delete [] temp1.str;
  18.    temp1.len = st.len+len;
  19.    temp1.str = new char[temp1.len+1];
  20.    strcpy(temp1.str,str);
  21.    for (int i=len; i< temp1.len; i++)
  22.       temp1.str[i]=st.str[i-len];
  23.    temp1.str[temp1.len+1]='\0';  // temp1.str[temp1.len] is the last
  24.                  // byte in temp1.str!!!
  25.    cout << temp1.len << " length1st\n";
  26.    return temp1;
  27. }
  28. And why don't you use strcat?
  29. Like:    strcpy(temp1.str, str);
  30.     strcat(temp1.str, st.str);  // That's enough!
  31.     return temp1;
  32.  
  33. Another hint for this function:
  34. String String::operator+(const char * s) const
  35. {
  36.    String tempo;
  37.    delete [] tempo.str;
  38.    tempo.len=strlen(s);
  39.    tempo.str = new char[tempo.len+1];
  40.    strcpy(tempo.str,s);
  41.    String temp = *this+tempo;
  42.    cout << temp.len << " length2nd\n";
  43.    return temp;
  44. }
  45. I would write this like:
  46. String String::operator+(const char * s) const
  47. {
  48.    return *this + String(s);
  49. }
  50.  
  51. To the question of the temporaries:
  52. Normally these are destroyed after the statement is executed.
  53. But of course, there is no need for the compiler to destroy a temporary
  54. befor exit of the current block. But after exiting the
  55. current block, the temporaries should have been deleted!
  56.  
  57. I hope you code will be running soon!
  58. Bye.
  59.  
  60.